﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Me.Amon.Open;
using Me.Amon.Open.V1.App.Pcs;
using Me.Amon.Pcs.M;
using Me.Amon.Pcs.V;

namespace Me.Amon.Pcs
{
    /// <summary>
    /// WPcs.xaml 的交互逻辑
    /// </summary>
    public partial class WPcs : Window
    {
        private Dictionary<string, ImageSource> _Icons;
        private ImageSource _Unknown;
        private ImageSource _Folder;
        private ImageSource _File;
        private TabHead _UcTab;

        public WPcs()
        {
            InitializeComponent();

            //BorderBrush = new SolidColorBrush(Colors.Silver);
            //BorderThickness = new Thickness(4);
            //Background = new LinearGradientBrush(Colors.DarkGray, Colors.DarkRed, 90);
            //GdMain.Background = new ImageBrush();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            Init();
        }

        public void Init()
        {
            _UcTab = new TabHead(this);
            _UcTab.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
            _UcTab.VerticalAlignment = System.Windows.VerticalAlignment.Top;
            _UcTab.Margin = new Thickness(60, 0, 0, 0);
            _UcTab.Height = 24;
            GdTab.Children.Add(_UcTab);

            OAuthConsumer consumer = new OAuthConsumer();
            consumer.consumer_key = "xcWPaz75PSRDOWBM";
            consumer.consumer_secret = "DU5ZYaCK0cRlsMTj";
            KuaipanClient client = new KuaipanClient(consumer);
            client.Verify();
            var ntp = new TabPage(this, client);
            ntp.Margin = new Thickness(0);
            ntp.Init();
            _UcTab.AddPage("", ntp);

            _Icons = new Dictionary<string, ImageSource>();
            _Unknown = new BitmapImage(new Uri(@"F:\OpenPCS\Unknown.png", UriKind.RelativeOrAbsolute));
            _Folder = new BitmapImage(new Uri(@"F:\OpenPCS\Folder.png", UriKind.RelativeOrAbsolute));
            _File = new BitmapImage(new Uri(@"F:\OpenPCS\File.png", UriKind.RelativeOrAbsolute));

            string name;
            foreach (string tmp in Directory.GetFiles(@"F:\OpenPCS\OpenPCS\Ico", "*.png"))
            {
                name = Path.GetFileNameWithoutExtension(tmp);
                if (string.IsNullOrEmpty(name))
                {
                    continue;
                }

                _Icons[name] = new BitmapImage(new Uri(tmp, UriKind.RelativeOrAbsolute));
            }
        }

        public ImageSource GetIcon(string key)
        {
            if (string.IsNullOrEmpty(key) || !_Icons.ContainsKey(key))
            {
                return _Unknown;
            }

            int idx = 0;
            string ext;
            while (true)
            {
                idx = key.IndexOf('.', idx);
                if (idx < 0)
                {
                    return _File;
                }
                ext = key.Substring(idx + 1);
                if (_Icons.ContainsKey(ext))
                {
                    return _Icons[key];
                }
            }
        }

        public void ShowPage(TabPage page)
        {
            GdNtp.Children.Clear();
            GdNtp.Children.Add(page);
        }

        public CsMeta SelectedMeta { get; set; }

        public EPcs Operation { get; set; }

        public void CutMeta()
        {
            if (_UcTab.SelectedPage != null)
            {
                _UcTab.SelectedPage.CutMeta();
            }
        }

        public void CopyMeta()
        {
            if (_UcTab.SelectedPage != null)
            {
                _UcTab.SelectedPage.CopyMeta();
            }
        }

        public void PasteMeta()
        {
            if (_UcTab.SelectedPage != null)
            {
                _UcTab.SelectedPage.PasteMeta();
            }
        }

        #region 事件处理
        private void Window_MouseDown(object sender, MouseButtonEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                DragMove();
            }
        }

        private void BnMin_Click(object sender, RoutedEventArgs e)
        {
            WindowState = WindowState.Minimized;
        }

        private void BnMax_Click(object sender, RoutedEventArgs e)
        {
            if (WindowState == WindowState.Maximized)
            {
                WindowState = WindowState.Normal;
            }
            else
            {
                WindowState = WindowState.Maximized;
            }
        }

        private void BnClose_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }
        #endregion
    }
}
